define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'manufacture/stock/index' + location.search,
                    add_url: 'manufacture/stock/add',
                    edit_url: 'manufacture/stock/edit',
                    del_url: 'manufacture/stock/del',
                    multi_url: 'manufacture/stock/multi',
                    table: 'stock',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'material.material_name', title: __('物料名称'), operate: false},
                        {field: 'material.material_code', title: __('物料编码'), operate: false},
                        {field: 'warehouse.warehouse_name', title: __('仓库'), operate: false},
                        {field: 'quantity', title: __('库存数量'), align: 'center'},
                        {field: 'warning_quantity', title: __('预警数量'), align: 'center'},
                        {field: 'status', title: __('状态'), formatter: function(value, row, index) {
                            if (row.quantity <= row.warning_quantity) {
                                return '<span class="label label-danger">库存不足</span>';
                            } else {
                                return '<span class="label label-success">正常</span>';
                            }
                        }},
                        {field: 'createtime', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('操作'), table: table, events: Table.api.events.operate, formatter: function(value, row, index) {
                            var table = this.table;
                            var buttons = [];
                            buttons.push({
                                name: 'inbound',
                                text: __('入库'),
                                title: __('物料入库'),
                                classname: 'btn btn-xs btn-success btn-dialog',
                                icon: 'fa fa-plus',
                                url: 'manufacture/stock/inbound/ids/{ids}',
                                callback: function (data) {
                                    table.bootstrapTable('refresh');
                                }
                            });
                            buttons.push({
                                name: 'outbound',
                                text: __('出库'),
                                title: __('物料出库'),
                                classname: 'btn btn-xs btn-warning btn-dialog',
                                icon: 'fa fa-minus',
                                url: 'manufacture/stock/outbound/ids/{ids}',
                                callback: function (data) {
                                    table.bootstrapTable('refresh');
                                }
                            });
                            return Table.api.formatter.operate.call(this, value, row, index, buttons);
                        }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        inbound: function () {
            Controller.api.bindevent();
        },
        outbound: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
